package com.j2boot.semaphore;

import java.util.concurrent.atomic.AtomicInteger;

/**
 * @author yichengxian
 */
public class sync {


    /**
     * 信号量实现同步操作
     * @param args
     */
    public static void main(String[] args) {

        intSemaphore intSemaphore = new intSemaphore();
        //初始化互斥量为0
        AtomicInteger s = new AtomicInteger(0) ;

        //线程1
        Thread thread1 = new Thread(() -> {
            System.out.println("大家好:我是线程1");
            intSemaphore.wait(s);
            System.out.println("已经同步~");
        });

        //线程2
        Thread thread2 = new Thread(() -> {
            System.out.println("大家好:我是线程2");
            intSemaphore.signal(s);
            System.out.println("s=>"+s.get());
        });


        thread1.start();
        thread2.start();



    }
}
